﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client
{
    /// <summary>
    /// File: Client_Handler.cs
    /// Author: James Dibble 10009689, Harry Winser 10016143
    /// Last Revision: 20/03/12
    /// </summary>
    class ClientRequest
    {
        #region Attributes
        private String _strHost;
        private int _intPort;
        private String[] _strAdditionsArr;
        private String _strQuery;
        private String _strRequestedPot;
        #endregion

        #region Constructor
        /// <summary>
        /// Parses the URL, contains data for each part.
        /// </summary>
        /// <param name="requestURL">Input URL</param>
        public ClientRequest(String requestURL)
        {
            String[] strURLParts = requestURL.Split('/');
            int intCount = 0;
            foreach (String strThisPart in strURLParts)
            {
                switch (intCount) // dwecoding and parsing the URL
                {
                    case 0:
                        if(strURLParts[intCount] != "coffee:") throw new ClientException("You must start your URL with 'coffee'.  How else would you get a cup?", ClientException.ExceptionType.NOCOFFEE);
                        break;
                    case 2:
                        if (strURLParts[intCount].Contains(':')) //checks for port after Host
                        {
                            _intPort = Convert.ToInt32(strURLParts[intCount].Substring(strURLParts[intCount].IndexOf(':') + 1));
                            _strHost = strURLParts[intCount].Substring(0, strURLParts[intCount].IndexOf(':'));
                        }
                        else _strHost = strURLParts[intCount];
                        break;
                    case 3:
                        if (strURLParts[intCount] == "") _strRequestedPot = ""; // no Additions or Pot Designator
                        if (strURLParts[intCount].Contains('?') && strURLParts[intCount].IndexOf('?') != 0) //no potDes
                        {
                            _strQuery = strURLParts[intCount].Substring(strURLParts[intCount].IndexOf('?') + 1); // creates Query String
                            _strRequestedPot = strURLParts[intCount].Substring(0, strURLParts[intCount].IndexOf('?') );
                            if(_strQuery.ToLower() != "propfind") _strAdditionsArr = _strQuery.Split('&'); // Check for PropFind
                        }
                        else if (strURLParts[intCount].Contains('?') && strURLParts[intCount].IndexOf('?') == 0)// No Additions/No Potdes, Create Propfind
                        {
                            _strRequestedPot = "";// No potDes
                            _strQuery = strURLParts[intCount].Substring(strURLParts[intCount].IndexOf('?') + 1);
                            if (_strQuery.ToLower() != "propfind") _strAdditionsArr = _strQuery.Split('&');
                        }
                        break;
                }
                intCount++;
            }
        }
        #endregion

        #region Properties
        public String Host
        {
            get { return _strHost; }
        }

        public int Port
        {
            get { return _intPort; }
        }

        public String[] Additions
        {
            get { return _strAdditionsArr; }
        }

        public String Pot
        {
            get { return _strRequestedPot; }
        }
        #endregion
    }
}
